<?php

namespace app\admin\controller;

use app\admin\model\ReasonModel;
use app\admin\model\RecordModel;
use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use app\admin\model\ScoreModel;
use app\admin\model\StudentModel;
use cmf\controller\AdminBaseController;
use think\Controller;
use think\Db;
use think\db\Query;
use think\Request;

class RecordController extends AdminBaseController
{
    public function index()
    {
       $name=$this->request->param('name');
        $record=Db::name('record')
            ->join('cmf_student','cmf_student.id=cmf_record.student_id','LEFT')
            ->join('cmf_score','cmf_score.id=cmf_record.score_id','LEFT')
            ->join('cmf_reason','cmf_reason.id=cmf_record.reason_id','LEFT')
            ->field('cmf_record.*,cmf_student.name as student_name,cmf_score.name as score_name,cmf_reason.name as reason_name')
            ->where(function (Query $query) use ($name){
                if ($name){
                    $query->where('cmf_student.name','like',"%$name%");
                }
            })
            ->order('id desc')
            ->paginate(2,false,$this->request->param());
        $page=$record->render();
        return $this->fetch('',['page'=>$page,'record'=>$record]);
    }
    public function add()
    {
        $reason=ReasonModel::select();
        $score=ScoreModel::select();
        $school=SchoolModel::select();
        return $this->fetch('add',['reason'=>$reason,'score'=>$score,'school'=>$school]);
    }
    public function addTwo(){
        $data=input();
        $data['create_time']=date('Y-m-d H:i:s',time());
        unset($data['photo_name']);
        $add=Db::name('record')->insert($data);
        if ($add){
            $this->success('添加成功','record/index');
        }else{
            $this->error('添加失败');
        }
    }
    /*public function addPost()
    {
        $file = $_FILES['file'];
        // 截取后缀名
        $ext = strchr($file['name'], '.');
        // 构建鉴权对象
        $auth = new Auth($this->accessKey, $this->secretKey);
        // 生成上传 Token
        $token = $auth->uploadToken($this->bucket);
        // 要上传文件的本地路径
        $filePath = $file['tmp_name'];
        // 上传到七牛后保存的文件名
        $key = time().uniqid().$ext;
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return $err; // 失败
        } else {
            return $key; // 成功，$key 就是在七牛云的文件名，加上你在七牛云的外链地址就可以访问到文件
        }
    }*/
    public function getRoom(){
        $schoolId=input('school_id');
        $room=Db::name('room')->where('school_id',$schoolId)->select();
        if ($room){
            $options=["<option value=''>--请选择--</option>"];
            foreach ($room as $v){
                $opt='<option class="form-control" value="'.$v['id'].'">'.$v['name'].'</option>';
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }
    public function getStudent(){
        $roomId=input('room_id');
        $student=Db::name('student')->where('room_id',$roomId)->select();
        if ($student){
            $options=["<option value=''>--请选择--</option>"];
            foreach ($student as $v){
                $opt='<option class="form-control" value="'.$v['id'].'">'.$v['name'].'</option>';
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }
    public function edit($id)
    {
        $id = input('id');
        $record = RecordModel::get($id);
        $school = SchoolModel::all();
        $reason = ReasonModel::all();
        $score = ScoreModel::all();
        $room_id = StudentModel::where('id', $record->student_id)->value('room_id');
        $school_id = RoomModel::where('id', $room_id)->value('school_id');
        $room = RoomModel::where('school_id', $school_id)->select();
        $student = StudentModel::where('room_id', $room_id)->select();
        return $this->fetch('', [
            'record' => $record,
            'school' => $school,
            'reason' => $reason,
            'score' => $score,
            'room_id' => $room_id,
            'school_id' => $school_id,
            'room' => $room,
            'student' => $student
        ]);

    }
    public function editPost()
    {
        $data = input();
        $edit=RecordModel::update($data,['id'=>$data['id']]);
        if ($edit) {
            $this->success("修改成功！", "record/index");
        }else{
            $this->error("修改失败！");
        }
    }
    public function delete($id)
    {
        $del=RecordModel::destroy($id);
        if ($del){
            $this->success('删除成功','record/index');
        }else{
            $this->error('删除失败');
        }
    }
}
